package megajournal.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import megajournal.domain.Lesson;
import megajournal.domain.Skip;
import megajournal.domain.Student;
import megajournal.view.ocDialog;

/**
 *
 * @author cska
 * 
 */

public class SkipDlg extends JDialog implements ocDialog {

    protected Skip skip;
    protected List<Student> students;
    public boolean fl;
    protected GridBagConstraints c;
    protected JPanel bp1;
    protected JButton ok, cancel;
    protected JComboBox studentBox;
    protected JComboBox first;
    protected JComboBox second;
    protected JComboBox cause;
    protected JLabel l1, l2, l3, l4;

    public SkipDlg(JFrame owner, boolean modal, String title, Skip skip, Lesson lesson, List<Student> students) {
        super(owner, title, modal);
        setLayout(new GridBagLayout());

        if (skip != null)
            this.skip = skip;
        else
            this.skip = new Skip(0, 0, lesson, students.get(0), 0);

        c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        l1 = new JLabel("Студент:", JLabel.RIGHT);
        add(l1,c);
        c.gridy = 1;
        l2 = new JLabel("Первый час:", JLabel.RIGHT);
        add(l2,c);
        c.gridy = 2;
        l3 = new JLabel("Второй час:", JLabel.RIGHT);
        add(l3,c);
        c.gridy = 3;
        l4 = new JLabel("Причина:", JLabel.RIGHT);
        add(l4,c);

        c = new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        this.students = students;
        studentBox = new JComboBox();
        for (Student student : students)
            studentBox.addItem(student.getFio());
        add(studentBox,c);
        c.gridy = 1;
        String first_s[] = {"не был", "был"};
        first = new JComboBox(first_s);
        first.setSelectedIndex(this.skip.getFirst());
        add(first,c);
        c.gridy = 2;
        String second_s[] = {"не был", "был"};
        second = new JComboBox(second_s);
        second.setSelectedIndex(this.skip.getSecond());
        add(second,c);
        c.gridy = 3;
        String cause_s[] = {"неув.", "ув."};
        cause = new JComboBox(cause_s);
        cause.setSelectedIndex(this.skip.getCause());
        add(cause,c);

        c = new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        bp1= new JPanel(new GridBagLayout());
        add(bp1,c);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        bp1.add(ok,c);
        c.gridx = 1;
        c.weightx = 0.0;
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        bp1.add(cancel,c);

        setSize(400,220);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width-400)/2, (screen.height-220)/2);
        setResizable(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setVisible(true);
    }

    public void ok() {
        fl = true;
        skip.setFirst(first.getSelectedIndex());
        skip.setSecond(second.getSelectedIndex());
        skip.setCause(cause.getSelectedIndex());
        skip.setStudent(students.get(studentBox.getSelectedIndex()));
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

    public Skip getSkip() {
        return skip;
    }
    
}
